package ma.sqli.vehicles;

public class Doors {

	private static String[] closedDoorsNumber;

	public  String getClosedDoorsReport(String closedDoors, int numberOfDoors, String consumptionReport) {

		String[] closedDoorsSepereted = closedDoors.split(" ");
		int numberOfClosedDoors = closedDoorsSepereted.length;
		closedDoorsNumber = setClosedDoors(numberOfDoors, closedDoors);

		if (numberOfClosedDoors == numberOfDoors || (closedDoors.length() == 0)) 
		{
			return "DOORS OK, MOVING. " + consumptionReport;
		} 
		else 
		{
			setOpenedDoors();
		}

		return "DOORS KO, BLOCKED \n  _\n " + closedDoorsNumber[0] + " "
				+ closedDoorsNumber[1] + "\n " + closedDoorsNumber[2] + "_"
				+ closedDoorsNumber[3];
	}

	public  String[] setClosedDoors(int numberOfDoors, String closedDoors) {

		String[] closedDoorsNumbers = new String[numberOfDoors];

		if (closedDoors.length() > 0) {
			String[] closedDoorsString = closedDoors.split(" ");

			for (String doorNumber : closedDoorsString) {
				int i = Integer.parseInt(doorNumber);
				closedDoorsNumbers[i - 1] = DoorStatut.doorClosed.toString();
			}
		}
		return closedDoorsNumbers;

	}

	private static void setOpenedDoors() {
		for (int i = 0; i < closedDoorsNumber.length; i++) {

			if (closedDoorsNumber[i] == null && (i == 0 || i == 2))
				closedDoorsNumber[i] = DoorStatut.rightDoorNotClosed.toString();
			else if (closedDoorsNumber[i] == null && (i == 1 || i == 3))
				closedDoorsNumber[i] = DoorStatut.leftDoorNotClosed.toString();
		}
	}
}
